using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Client;
using FreeFlow.Client.Events;

namespace FreeFlow.WinFormsControls
{
	/// <summary>
	/// WinForms control to display a list of blank forms.
	/// </summary>
  public class BlankFormsGrid : Grid
  {
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    /// <summary>
    /// Creates a new <see cref="BlankFormsList"/> instance.
    /// </summary>
    public BlankFormsGrid()
    {
      // This call is required by the Windows.Forms Form Designer.
      InitializeComponent();

      dataGrid.DoubleClick += new EventHandler(dataGrid_DoubleClick);

      CreateColumnStyles();
    }

    private void CreateColumnStyles()
    {
      // create styles
      DataGridTableStyle DcStyle = new DataGridTableStyle();
      DcStyle.MappingName = BlankFormsList.ListDataTableName;

      DcStyle.GridColumnStyles.Add(CreateColumn(BlankFormsList.GroupColumn, "Group"));
      DcStyle.GridColumnStyles.Add(CreateColumn(BlankFormsList.ProcedureColumn, "Procedure"));
      DcStyle.GridColumnStyles.Add(CreateColumn(BlankFormsList.ProcessColumn, "Process"));
      DcStyle.GridColumnStyles.Add(CreateColumn(BlankFormsList.ActionColumn, "Action"));
      DcStyle.GridColumnStyles.Add(CreateColumn(BlankFormsList.DescriptionColumn, "Description"));
      
      dataGrid.TableStyles.Add(DcStyle);
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      // 
      // BlankFormsList
      // 
      this.Name = "BlankFormsList";
      this.Size = new System.Drawing.Size(528, 288);

    }
		#endregion

    private FormType formType;
    /// <summary>
    /// Gets or sets the type of blank form to display.
    /// </summary>
    [Description("The type of blank form the list will display")]
    [Category(Constants.FreeFlowCategory)]
    public FormType BlankFormType
    {
      get { return formType; }
      set { formType = value; }
    }

    /// <summary>
    /// Populates the grid.
    /// </summary>
		public void PopulateGrid()
    {
      BlankFormsList blankForms = new BlankFormsList();
      blankForms.Connection = Connection;
      blankForms.BlankFormType = formType;
      blankForms.RefreshItems();
      dataGrid.DataSource = blankForms.ListDataTable;
    }

    /// <summary>
    /// Event handler to indicate a blank form should be opened.
    /// </summary>
    [Category(Constants.FreeFlowCategory)]
    public event EventHandler<OpenBlankFormEventArgs> OpenBlankForm;

    private void dataGrid_DoubleClick(object sender, EventArgs e)
    {
      if (OpenBlankForm != null)
      {
        BindingManagerBase bm = dataGrid.BindingContext[dataGrid.DataSource];
        DataRowView row = (DataRowView) bm.Current;
        string processName = row[BlankFormsList.ProcessColumn].ToString();
        string actionName = row[BlankFormsList.ActionColumn].ToString();
        OpenBlankForm(this, new OpenBlankFormEventArgs(processName, actionName));
      }
    }
	}
}
